package com.yinhe.music.yhmusic.application;

import android.app.Application;
import android.content.Context;
import android.os.Environment;
import android.support.multidex.MultiDex;
import android.text.TextUtils;

import com.tencent.bugly.Bugly;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.message.IUmengRegisterCallback;
import com.umeng.message.PushAgent;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;
import com.yinhe.music.yhmusic.cache.CacheManager;
import com.yinhe.music.yhmusic.constants.BasicConfig;
import com.yinhe.music.yhmusic.db.DataBaseAccessor;
import com.yinhe.music.yhmusic.log.MLog;
import com.yinhe.music.yhmusic.network.NetworkManager;
import com.yinhe.music.yhmusic.utils.PackageUtil;
import com.yinhe.music.yhmusic.utils.PlayingCoverLoader;
import com.yinhe.music.yhmusic.utils.Preferences;

import org.android.agoo.huawei.HuaWeiRegister;
import org.android.agoo.xiaomi.MiPushRegistar;

import java.io.File;


/**
 * 自定义Application
 * Created by wcy on 2015/11/27.
 */
public class MusicApplication extends Application {
    {
        PlatformConfig.setWeixin("wx9fab90b12ca1ed73", "344bba4d664717edfe2a637050c6d7af");
        PlatformConfig.setQQZone("1106334186", "HvogC0ZLLBq9jmXf");
        PlatformConfig.setSinaWeibo("2255798513", "3dc282a45ba34996d9ead83cc429749b", "https://api.weibo.com/oauth2/default.html");
    }

    private static int MAX_MEM = (int) Runtime.getRuntime().maxMemory() / 4;
    public static final String YINHE_DIR_NAME = "yinhe";

    private static MusicApplication instance;
    private static Context context;

    public static MusicApplication getInstance() {
        return instance;
    }

    public static Context getContext() {
        return context;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    @Override
    public void onCreate() {
        super.onCreate();

        String logPath = getSDPath();
        if (!TextUtils.isEmpty(logPath)) {
            logPath = logPath + "/yinhe/log";
            MLog.initialize(logPath);
        }

        context = getApplicationContext();
        instance = this;

        Bugly.init(getApplicationContext(), "474021fe4c", true);

        NetworkManager.getInstance().init();

        PackageUtil.init(this);
        AppCache.init(this);

        UMShareAPI.get(this);

        initUmengPush();

        CacheManager.getInstance().init(this);
        initDB();
        initBaseConfig();

//        if (BuildConfig.DEBUG) {
//            LeakCanary.install(this);
//        }

        initAppStatusListener();
    }

    public String getSDPath() {
        File sdDir = null;

        String state = Environment.getExternalStorageState();
        if (state == null) {
            return null;
        }

        boolean sdCardExist = state.equals(android.os.Environment.MEDIA_MOUNTED);//判断sd卡是否存在
        if (sdCardExist) {
            sdDir = Environment.getExternalStorageDirectory();//获取跟目录
        }
        if (sdDir == null) {
            return null;
        }
        return sdDir.toString();
    }

    private void initAppStatusListener() {
//        ForegroundCallbacks.init(this).addListener(new ForegroundCallbacks.Listener() {
//            @Override
//            public void onBecameForeground() {
//                MLog.info("WsManager","应用回到前台调用重连方法");
//                WsManager.getInstance().reconnect();
//            }
//
//            @Override
//            public void onBecameBackground() {
//
//            }
//        });
    }

    private void initBaseConfig() {
        PlayingCoverLoader.getInstance().initCacheDir(this);
        BasicConfig.getInstance().setAppContext(this);
        BasicConfig.getInstance().startExternalState();
        BasicConfig.getInstance().setRootDir(YINHE_DIR_NAME);
        BasicConfig.getInstance().setLogDir(YINHE_DIR_NAME + File.separator + "logs" + File.separator);
        BasicConfig.getInstance().setConfigDir(YINHE_DIR_NAME + File.separator + "config" + File.separator);
        BasicConfig.getInstance().setMusicDownLoadDir(YINHE_DIR_NAME + File.separator + "music" + File.separator);
        BasicConfig.getInstance().setLyricDir(YINHE_DIR_NAME + File.separator + "Lyric" + File.separator);
        BasicConfig.getInstance().setImageDir(YINHE_DIR_NAME + File.separator + "image" + File.separator);
    }

    public static void initDB() {
        DataBaseAccessor.getInstance().initDBHelper(instance, Preferences.getLoginSn().length() > 0
                ? Preferences.getUid() + ".db"
                : "visitor.db");
    }

    private void initUmengPush() {
        UMConfigure.init(this, UMConfigure.DEVICE_TYPE_PHONE, "e95bceb0dea8dc4ee35b013a86d0695b");

        MiPushRegistar.register(this, "2882303761517817037", "5581781760037");
        HuaWeiRegister.register(this);

        PushAgent mPushAgent = PushAgent.getInstance(this);
        //注册推送服务，每次调用register方法都会回调该接口
        mPushAgent.register(new IUmengRegisterCallback() {
            @Override
            public void onSuccess(String deviceToken) {
                //注册成功会返回device token

            }

            @Override
            public void onFailure(String s, String s1) {
            }
        });
    }
}

